//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "event\fn_eventObjectiveLeadFailed.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

if (isServer) then {
	private ["_object","_action","_parameters","_destination_msg","_die_msg","_shared_destination","_shared_alive","_destination_code","_die_code","_destination","_evt_idx"];
	DEFAULT_PARAM(_params,0,[]);
	DEFAULT_PARAM(_action_id,1,-1);
	
	_object = GETVAL(_params,0,objNull);
	_killer = GETVAL(_params,1,objNull);
	if (!isnull _object) then {
		_action = [_object,_action_id] call FFUNC(actionGet);
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
		_instance_id = [_object] call FFUNC(getInstanceID);
		_status = [_object,_action,"LEAD_KILLED_STATUS",-1] call FFUNC(actionGetLocal);
		if (!isNil "_status" && _status < 0) then {
			[_object,_action,"LEAD_KILLED_STATUS",1] call FFUNC(actionSetLocal);
			_destination_msg = [_parameters, "DEST_MSG", ""] call CFUNC(hashGet);
			_die_msg = [_parameters, "DIE_MSG", ""] call CFUNC(hashGet);
			_shared_destination = [_parameters, "SHARED_DEST_VAR", ""] call CFUNC(hashGet);
			_shared_alive = [_parameters, "SHARED_ALIVE_VAR", ""] call CFUNC(hashGet);
			_destination = [_parameters, "DESTINATION", []] call CFUNC(hashGet);
			_destination_code = [_parameters, "DEST_CODE", ""] call CFUNC(hashGet);
			_die_code = [_parameters, "DIE_CODE", ""] call CFUNC(hashGet);
			if (typename _die_msg == typename "") then {_die_msg = [_die_msg];};
			if (typename _destination_msg == typename "") then {_destination_msg = [_destination_msg];};
			
			if (!isnull (group _object)) then {
				if (isplayer (leader (group _object))) then {
					[_object] joinSilent grpNull;
					TRACE_1("Degrouped AI",group _object);
				};
			};
			_evt_idx = [_object,_action,"LEAD_KILLED_EVENT",-1] call FFUNC(actionGetLocal);
			If (!isNil "_evt_idx" && _evt_idx >= 0) then {
				_object removeEventHandler ["killed",_evt_idx];
				TRACE_2("Removed Event",_evt_idx,"LEAD_KILLED_EVENT");
				[_object,_action,"LEAD_KILLED_EVENT",-1] call FFUNC(actionSetLocal);
			};
			if (!isNil "_die_msg") then {
				if (count _die_msg > 0) then {[QGVAR(all_message),[_die_msg,"all",objNull,"hq"],THIS_FILE_] call NFUNC(callEvent);};
			};
			if (!isNil "_shared_destination") then {
				if (_shared_destination != "") then {
					[_instance_id,_shared_destination,false] call FFUNC(setMissionJIPVar);
					[QGVAR(all_var), [_instance_id,_shared_destination,false],THIS_FILE_] call NFUNC(callEvent);
				};
			};
			if (!isNil "_shared_alive") then {
				if (_shared_alive != "") then {
					[_instance_id,_shared_alive,alive _object] call FFUNC(setMissionJIPVar);
					[QGVAR(all_var), [_instance_id,_shared_alive,alive _object],THIS_FILE_] call NFUNC(callEvent);
				};
			};
			if (_die_code != "") then {
				[QGVAR(all_execute),[_die_code,_object,_action],THIS_FILE_] call NFUNC(callEvent);
			};
		};
	};
} else {
	[QGVAR(s_execute),[QUOTE(FFUNC(eventObjectiveLeadFailed)),_this],THIS_FILE_] call NFUNC(callEvent);
	_evt_idx = [_object,_action,"LEAD_KILLED_EVENT",-1] call FFUNC(actionGetLocal);
	if (!isNil "_evt_idx" && _evt_idx >= 0) then {
		_object removeEventHandler ["killed", _evt_idx];
		[_object,_action,"LEAD_KILLED_EVENT",-1] call FFUNC(actionSetLocal);
	};
};

TRACE("End");
TMFPROFILERSTOP;